package com.bw.exam.controller;

import com.alibaba.fastjson.JSON;
import com.bw.exam.pojo.ItemCat;
import com.bw.exam.pojo.response.R;
import com.bw.exam.service.ItemCatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: liuzhiping
 * @date: 2024/7/25 11:49
 **/
@Slf4j
@RestController
@RequestMapping("/platform/itemCat")
public class ItemCatController {

    @Resource
    com.bw.exam.mapper.ItemCatMapper itemCatMapper;

    @Autowired
    private ItemCatService itemCatService;

    /***
     * 左链接的方式
     * @return
     */
    @RequestMapping("tree")
    public R tree(){
        List<ItemCat> list = itemCatMapper.selectItemTree();
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        return R.success(map);
    }

    /***
     *
     * @return
     */
    @RequestMapping("tree1")
    public R tree1(){
        List<ItemCat> list = itemCatService.listTree1();
        return R.success(list);
    }

    @RequestMapping("/insert")
    public  R add(@RequestBody ItemCat itemCat){
        log.info("商品类目 itemCat:"+ JSON.toJSONString(itemCat));
        if(itemCat.getParentId() == null || itemCat.getParentId() == 0){
            return R.fail();
        }
        boolean boo =  this.itemCatService.saveOrUpdate(itemCat);
        return R.success();
    }

    @GetMapping("/findById/{id}")
    public R findById(@PathVariable  Long id){
        log.info("商品类目 id:"+ id);
        return R.success(itemCatService.getById(id));
    }

    @GetMapping("/delById/{id}")
    public R delById(@PathVariable  Long id){
        log.info("删除商品类目 id:"+ id);
        itemCatService.delById(id);
        return R.success();
    }
}
